package com.example.zsfinacialmanage.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.example.zsfinacialmanage.R;
import com.example.zsfinacialmanage.db.MyDBHelper;
import com.example.zsfinacialmanage.view.MainActivity;

import java.text.SimpleDateFormat;
import java.util.Date;

public class NewInComeActivity extends AppCompatActivity {
    //1 定义对象
    EditText et_money,et_time,et_payer,et_remark;
    Spinner sp_type;
    Button bt_sava,bt_cancel;
    MyDBHelper mhelper;
    SQLiteDatabase db;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_in_come);
        et_time=findViewById(R.id.et_time_newin);
        //2 绑定控件
        initView();
        //3保存按钮功能的实现
        btnSave();

        //4 取消按钮功能的实现
        btnCancel();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String currentTime = sdf.format(new Date());

        // 设置hint为当前时间
        et_time.setHint(currentTime);

    }


    //2 绑定控件-------------------代码
    private void initView() {
        et_money=findViewById(R.id.et_money_newin);

        sp_type=findViewById(R.id.sp_type_newin);
        et_payer=findViewById(R.id.et_payer_newin);
        et_remark=findViewById(R.id.et_remark_newin);
        bt_sava=findViewById(R.id.bt_save_newin);
        bt_cancel=findViewById(R.id.bt_cancel_newin);
        mhelper=new MyDBHelper(NewInComeActivity.this);
        db=mhelper.getWritableDatabase();
    }

    //3保存按钮功能的实现--------代码
    private void btnSave() {

        bt_sava.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 获取当前时间
                long currentTime = System.currentTimeMillis();
                Date currentDate = new Date(currentTime);

                // 使用SimpleDateFormat格式化日期
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
                String formattedTime = sdf.format(currentDate);

                // 更新内容
                ContentValues values = new ContentValues();
                values.put("inmoney", et_money.getText().toString());
                values.put("intime", formattedTime); // 使用格式化后的时间
                values.put("intype", sp_type.getSelectedItem().toString());
                values.put("inpayer", "吴亚楠"); // 硬编码payer为"吴亚楠"
                values.put("inremark", "2021213860"); // 更新remark为指定值

                db.insert("in_come", null, values);
                Toast.makeText(NewInComeActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
                // 刷新本页面
                Intent intent = new Intent(NewInComeActivity.this, NewInComeActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }


    //4 取消按钮功能的实现--------代码
    private void btnCancel() {
        bt_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(NewInComeActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

}